package com.xxx.dorm.web.servlet;

import com.xxx.dorm.domain.Building;
import com.xxx.dorm.domain.Dormitory;
import com.xxx.dorm.domain.Record;
import com.xxx.dorm.domain.Student;
import com.xxx.dorm.service.BuildingService;
import com.xxx.dorm.service.DormitoryService;
import com.xxx.dorm.service.RecordService;
import com.xxx.dorm.service.StudentService;
import com.xxx.dorm.service.impl.BuildingServiceImpl;
import com.xxx.dorm.service.impl.DormitoryServiceImpl;
import com.xxx.dorm.service.impl.RecordServiceImpl;
import com.xxx.dorm.service.impl.StudentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@WebServlet("/record/*")
public class RecordServlet extends BaseServlet {

    private RecordService recordService = new RecordServiceImpl();
    private StudentService studentService = new StudentServiceImpl();
    private BuildingService buildingService = new BuildingServiceImpl();
    private DormitoryService dormitoryService = new DormitoryServiceImpl();

    /**
     * 查找
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String conditions = request.getParameter("conditions");
        String information = request.getParameter("information");
        String number = request.getParameter("number");
        List<Record> records = null;
        if(conditions != "" && information != "" && conditions != null && information != null){
            records = recordService.find(conditions,information);
        }else{
            records = recordService.findAll();
        }
        for(int i=0;i<records.size();i++){
            Student student = studentService.findById(records.get(i).getSid());
            Dormitory dormitory = dormitoryService.findByDid(student.getDid());
            Building building = buildingService.findByBid(dormitory.getBid());
            student.setBuilding(building);
            student.setDormitory(dormitory);
            records.get(i).setStudent(student);
        }
        request.setAttribute("records",records);
        request.setAttribute("mainRight","/WEB-INF/jsp/record/recordList.jsp");
        request.getRequestDispatcher("/WEB-INF/jsp/main.jsp").forward(request,response);
    }

    /**
     * 删除
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void deleteRecord(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        recordService.deleteRecord(Integer.valueOf(id));
        response.sendRedirect(request.getContextPath()+"/record/findAll");
    }

    /**
     * 添加
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     * @throws ParseException
     */
    public void addRecord(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, InvocationTargetException, IllegalAccessException, ParseException {
        String para = request.getParameter("para");
        if(para != null && "add".equals(para)){
            request.setAttribute("mainRight","/WEB-INF/jsp/record/addRecord.jsp");
            request.getRequestDispatcher("/WEB-INF/jsp/main.jsp").forward(request,response);
            return;
        }
        String stuCode = request.getParameter("stuCode");
        String dateStr = request.getParameter("date");
        String remark = request.getParameter("remark");
        Record record = new Record();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = sdf.parse(dateStr);
        List<Student> students = studentService.find("stuCode",stuCode);
        record.setSid(students.get(0).getId());
        record.setDate(date);
        record.setRemark(remark);
        recordService.addRecord(record);
        response.sendRedirect(request.getContextPath()+"/record/findAll");
    }

    /**
     * 查找学生信息
     * @param request
     * @param response
     * @throws IOException
     */
    public void findOne(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String stuCode = request.getParameter("stuCode");
        if(stuCode != null && !"".equals(stuCode)){
            List<Student> students = studentService.find("stuCode",stuCode);
            writeValue(students,response);
        }
    }

    /**
     * 通过id查找记录
     * @param request
     * @param response
     * @throws IOException
     * @throws ServletException
     */
    public void findRecord(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        String id = request.getParameter("id");
        Record record = recordService.findRecord(Integer.valueOf(id));
        Student student = studentService.findById(record.getSid());
        record.setStudent(student);
        request.setAttribute("record",record);
        request.setAttribute("mainRight","/WEB-INF/jsp/record/updateRecord.jsp");
        request.getRequestDispatcher("/WEB-INF/jsp/main.jsp").forward(request,response);
    }

    /**
     * 更新记录
     * @param request
     * @param response
     * @throws ParseException
     * @throws IOException
     */
    public void updateRecord(HttpServletRequest request, HttpServletResponse response) throws ParseException, IOException {
        String stuCode = request.getParameter("stuCode");
        String dateStr = request.getParameter("date");
        String remark = request.getParameter("remark");
        Integer id = Integer.valueOf(request.getParameter("id"));
        Record record = new Record();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = sdf.parse(dateStr);
        List<Student> students = studentService.find("stuCode",stuCode);
        record.setSid(students.get(0).getId());
        record.setDate(date);
        record.setId(id);
        record.setRemark(remark);
        record.setDate(date);
        recordService.updateRecord(record);
        response.sendRedirect(request.getContextPath()+"/record/findAll");
    }
}
